﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MusicAcademy
{
    public partial class frmNewPayment : Form
    {
        public int StudentID = -1;
        public int PaymentType;//1=> Creadit         &          2=> Debit
        public byte PersonType;//1=> Teacher         &          2=> Student            &           0=> All

        public frmNewPayment()
        {
            InitializeComponent();
        }

        private bool isValid()
        {
            bool valid = true;
            if (StudentID == -1)
                valid = Globals.SetError(txtStudent);
            else
                Globals.SetValid(txtStudent);

            if (Globals.IsInteger(txtAmount.Text) == false)
                valid = Globals.SetError(txtAmount);
            else
                Globals.SetValid(txtAmount);

            if (txtNumber.Text.Trim() == "" && txtNumber.Visible == true)
                valid = Globals.SetError(txtNumber);
            else
                Globals.SetValid(txtNumber);

            if (txtDescription.Text.Trim() == "")
                valid = Globals.SetError(txtDescription);
            else
                Globals.SetValid(txtDescription);
            return valid;
        }
        private void btnSelectStudent_Click(object sender, EventArgs e)
        {
            ListOfPersons p = new ListOfPersons();
            p.PersonType = PersonType;
            if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                StudentID = p.SelectedID;
                txtStudent.Text = p.FirstName + " " + p.LastName;
            }
        }

        private void cmbPaymentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbPaymentType.SelectedIndex == 0)
            {
                lblNumber.Visible = false;
                txtNumber.Visible = false;
            }
            else
            {
                lblNumber.Visible = true;
                txtNumber.Visible = true;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "Insert") == false)
            {
                MessageBox.Show("شما اجازه ی اضافه کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (isValid() == false)
                return;
            long credit;
            long debit;
            if (PaymentType == 1)
            {
                credit=long.Parse(txtAmount.Text);
                debit=0;
            }
            else{
                debit=long.Parse(txtAmount.Text);
                credit =0;
            }
            if (Globals.SaveTransaction(StudentID, Globals.Get10CharDate(), credit, debit, txtDescription.Text + (cmbPaymentType.SelectedIndex == 1 ? " - شماره فیش : " + txtNumber.Text : "")))
            {
                MessageBox.Show("پرداخت ثبت شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
                MessageBox.Show("خطا در ثبت پرداخت", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void frmNewPayment_Load(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "View") == false)
            {
                MessageBox.Show("شما اجازه ی دیدن این فرم را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
            }
            cmbPaymentType.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //cmbPaymentType.SelectedIndex = -1;
            //txtStudent.Text = "";
            //StudentID = -1;
            //txtAmount.Text = "";
            //txtDescription.Text = "";
            //txtNumber.Text = "";
            this.Close();
        }

        private void txtAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Globals.IsDigitOrCommand(e.KeyChar) == false)
                e.Handled = true;
        }
    }
}
